﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Prop;

namespace Web.pages
{
    public partial class usermanagement : System.Web.UI.Page
    {

        private Int32 idx = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            grvResult.RowDataBound += GrvResultOnRowDataBound;
            grvResult.PageIndexChanging += GrvResultOnPageIndexChanging;
            if (!IsPostBack)
            {
                OnReload();
            }
        }

        private void GrvResultOnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvResult.PageIndex = e.NewPageIndex;
            OnReload();
        }

        private void GrvResultOnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow) return;

            Label lbNo = (Label)e.Row.FindControl("lbNo");

            CheckBox authorize = (CheckBox) e.Row.FindControl("Authorize");

            if (lbNo != null)
                lbNo.Text = ((idx + 1) + (grvResult.PageIndex * grvResult.PageSize)).ToString();

            if (authorize != null)
            {
                if (DataBinder.Eval(e.Row.DataItem, "STATUS").ToString() == ((Int32) Core.Status.Use).ToString())
                {
                    authorize.Checked = true;
                }
                else
                {
                    authorize.Checked = false;
                }
            }

            idx += 1;
        }

        private void OnReload()
        {
            try
            {
                var o = Dao.USERDao.Init().SelectCondition(new USER()
                {
                    FNAME = txName.Text.Trim()
                    ,
                    LNAME = txName.Text.Trim()
                    ,
                    EMAIL = txEmail.Text.Trim()
                    ,
                    COMPANY = txCompany.Text.Trim()
                    ,
                    DEPT = txDept.Text.Trim()
                    ,
                    POSITION = txPosition.Text.Trim()
                });
                if (o.Count > 0)
                {
                    grvResult.DataSource = o.ToList();
                    grvResult.DataBind();
                }
            }
            catch (Exception ex)
            {
                Dao.TransactionlogDao.Init().Add(new Transactionlog()
                {
                    UID = ((USER)Session["UserInfo"]).UID
                    ,
                    EVENT = Core.Events.Error.ToString()
                    ,
                    LASTIP = Request.ServerVariables["REMOTE_ADDR"]
                    ,
                    UPDATE = DateTime.Now
                    ,
                    DESCRIPT = ex.Message
                });
            }
            upl.Update();
        }

        protected void btSearch_Click(object sender, EventArgs e)
        {
            OnReload();
        }

        protected void btSubmit_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < grvResult.Rows.Count ; i++)
            {
                CheckBox chk = (CheckBox)grvResult.Rows[i].FindControl("chk");
                if (chk != null)
                {
                    if (chk.Checked)
                    {
                        CheckBox authorize = (CheckBox)grvResult.Rows[i].FindControl("Authorize");
                        if (authorize != null)
                        {
                            if (authorize.Checked)
                            {
                                Dao.USERDao.Init()
                                    .StatusUpdate(((Label)grvResult.Rows[i].FindControl("UID")).Text, "1");
                            }
                            else
                            {
                                Dao.USERDao.Init()
                                    .StatusUpdate(((Label)grvResult.Rows[i].FindControl("UID")).Text, "2");
                            }
                        }
                    }

                }
            }
            OnReload();
        }    
    }
}